package cn.util.aop;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

@Component
@Aspect
/**
 * 使用方法
 *        前置通知:@Before
 *        返回通知:@AfterReturning
 *        后置通知:@After
 *        异常通知:@AfterThrowing
 *        环绕通知:@Around
 */
public class AuthorityAop {

    //绑定注解
    @Around("@annotation(authority)")
    public Object checkout(ProceedingJoinPoint point,Authority authority) throws Throwable {
        Long startTime=System.currentTimeMillis();
        //获取当前执行的方法名
        String returnType = point.getSignature().getName();
        Object[] args=point.getArgs();
        Subject subject=SecurityUtils.getSubject();
        Session session = subject.getSession();
        //已登录
        if(subject.getPrincipal()!=null) {
            //当前用户没有任何操作，才会开始计时退出时间
            session.setTimeout(1800000);
        }
        System.out.println("sessionId:" + session.getId());
        System.out.println("sessionHost:" + session.getHost());
        System.out.println("sessionTimeout:" + session.getTimeout());
        System.out.println("当前:"+args[0]);
        String key=authority.value();
        //第一个参数 为json数据
        if (args[0].equals("")){
            args[0]="{"+"\""+"value"+"\""+":"+"\""+"null"+"\""+"}";
        }
//            else if(args[0].toString().indexOf("&")>0) {
//                args[0]="{"+"\""+"value"+"\""+":"+"\""+"null"+"\""+"}";
//            }

        Object returnValue=point.proceed(args);
        Long stopTime=System.currentTimeMillis();
        Long time=(stopTime-startTime);
        System.out.println("进来消时:"+time+"毫秒");
        return returnValue;
    }
}
